/*
 * 版权所有 ( c ) 联想集团有限公司 2005-2009 保留所有权利。
 * 项目：	Lenovo Push Service
 * 文件名：	TestDemo.java
 * 版本:    1.0
 * 描述：	
 * 作者：	刘兵召
 * 日期：	May 10, 2010 
 * 修改历史：
 * 【时间】		【修改者】	【修改内容】
 */
package com.feng.demo.jakartacommons.configuration;

import java.awt.Dimension;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

public class TestDemo {
	
	public static void main(String[] args) throws ConfigurationException{
		testPropertiesConfiguration();
		testXmlConfiguration();
	}
	/**
	 * simple xml
	 * @throws ConfigurationException
	 */
	public static void testXmlConfiguration() throws ConfigurationException{
		XMLConfiguration config = new XMLConfiguration("com/feng/demo/jakartacommons/configuration/gui.xml");
		String backColor = config.getString("colors.background");
		String textColor = config.getString("colors.text");
		String linkNormal = config.getString("colors.link[@normal]");
		String defColor = config.getString("colors.default");
		int rowsPerPage = config.getInt("rowsPerPage");
		List buttons = config.getList("buttons.name");
		p("xml config info backColor: "+backColor);
		p("xml config info textColor: "+textColor);
		p("xml config info linkNormal: "+linkNormal);
		p("xml config info defColor: "+defColor);
		p("xml config info rowsPerPage: "+rowsPerPage);
		for(Object b : buttons)
			p("xml config info buttons: "+b);
	}
	/**
	 * Properties资源文件实例
	 * @throws ConfigurationException
	 */
	public static void testPropertiesConfiguration() throws ConfigurationException{
		/*
	 	If you do not specify an absolute path, the file will be searched automatically in the following locations: 
			in the current directory 
			in the user home directory 
			in the classpath 
		 */
		PropertiesConfiguration config = new PropertiesConfiguration("com/feng/demo/jakartacommons/configuration/usergui.properties");
		//
		//config.setBasePath("com/feng/demo/jakartacommons/configuration/");
		//config.setFileName("com/feng/demo/jakartacommons/configuration/usergui.properties");
		
		//Automatic Reloading
		config.setReloadingStrategy(new FileChangedReloadingStrategy());
		//config.setAutoSave(true);

		String backColor = config.getString("colors.background");
		Dimension size = new Dimension(config.getInt("window.width"),
		config.getInt("window.height"));
		String feng = config.getString("colors.feng");
		String bing = config.getString("window.bing");
		System.out.println(backColor+"  "+size +" " +feng + " " + bing);
		
		//Lists and arrays
		String[] colors = config.getStringArray("colors.pie");
		for(String pie : colors){
			System.out.println("colors.pie : "+pie);
		}
		List colorList = config.getList("colors.pie");
		for(Object blue : colorList){
			System.out.println("colors.blue : "+blue);
		}
		
		/*修改属性 The path must be absulate via test */
		config.setProperty("colors.background", "#FFFFFM");
		config.save("E:/code/MyTest/src/com/feng/demo/jakartacommons/configuration/usergui.backup.properties");
		System.out.println("After modify:"+config.getProperty("colors.background"));
		
		//Special Characters
		System.out.println(config.getString("key"));
		//Usual Characters
		System.out.println(config.getString("value"));
	}
	
	public static void p(String str){
		System.out.println(str);
	}
}
